package com.cloudspace.jindun.model;

import com.cloudspace.jindun.dao.ContactPerson;
import com.cloudspace.jindun.utils.ObjUtil;
import com.cloudspace.jindun.utils.PinyinUtils;
import com.google.gson.annotations.SerializedName;

import java.io.Serializable;

/**
 * Created by zengxianhua on 16/12/25.
 */

public class Person implements Serializable, Comparable{
    @SerializedName("userName")
    private String name;
    @SerializedName("organId")
    private String company;
    @SerializedName("userPhone")
    private String phoneNumber;
    @SerializedName("departmentId")
    private String department;
    @SerializedName("iconUrl")
    private String imageUrl;
    @SerializedName("sipAccout")
    private String sipAccount;
    private int isEmergencyContact;
    private String userId;

    @SerializedName("isExiPreLink")
    private String isExiPreLink;

    @SerializedName("departName")
    private String departName;
    @SerializedName("organName")
    private String organName;

    @SerializedName("remarkName")
    private String remarkName;

    public static Person constructFromDatabase(ContactPerson contactPerson){
        Person person = new Person();
        person.setImageUrl(contactPerson.getIconUrl());
        person.setSipAccount(contactPerson.getSipAccout());
        person.setUserId(contactPerson.getUserId());
        person.setName(contactPerson.getUserName());
        person.setDepartment(contactPerson.getDepartmentId());
        person.setPhoneNumber(contactPerson.getUserPhone());
        person.setCompany(contactPerson.getOrganId());
        person.setOrganName(contactPerson.getOrganName());
        person.setDepartName(contactPerson.getDepartName());
        person.setRemarkName(contactPerson.getRemarkName());
        person.setIsExiPreLink(contactPerson.getIsExiPreLink());
        return person;
    }

    public static ContactPerson contructContactPerson(Person person){
        ContactPerson contactPerson = new ContactPerson();
        contactPerson.setDepartmentId(person.getDepartment());
        contactPerson.setSipAccout(person.getSipAccount());
        contactPerson.setOrganId(person.getCompany());
        contactPerson.setIconUrl(person.getImageUrl());
        contactPerson.setUserId(person.getUserId());
        contactPerson.setUserName(person.getName());
        contactPerson.setOrganName(person.getOrganName());
        contactPerson.setDepartName(person.getDepartName());
        contactPerson.setRemarkName(person.getRemarkName());
        contactPerson.setUserPhone(person.getPhoneNumber());
        contactPerson.setIsExiPreLink(person.getIsExiPreLink());
        return contactPerson;
    }

    public String getName() {
        return name;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getSipAccount() {
        return sipAccount;
    }

    public void setSipAccount(String sipAccount) {
        this.sipAccount = sipAccount;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public int getIsEmergencyContact() {
        return isEmergencyContact;
    }

    public void setIsEmergencyContact(int isEmergencyContact) {
        this.isEmergencyContact = isEmergencyContact;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public int compareTo(Object another) {

        if (!(another instanceof  Person)){
            return -1;
        }

        Person anotherPerson = (Person) another;
        if (ObjUtil.isEmpty(another)
                || ObjUtil.isEmpty(anotherPerson.getName())
                || ObjUtil.isEmpty(name)
                ){
            return 0;
        }

        String firstLetter = PinyinUtils.getPinyinFirstLetter(name);
        String anotherLetter = PinyinUtils.getPinyinFirstLetter(anotherPerson.getName());

        if (ObjUtil.isEmpty(firstLetter) || ObjUtil.isEmpty(anotherLetter)){
            return 0;
        }

        return firstLetter.compareTo(anotherLetter);
    }

    public String getDepartName() {
        return departName;
    }

    public void setDepartName(String departName) {
        this.departName = departName;
    }

    public String getOrganName() {
        return organName;
    }

    public void setOrganName(String organName) {
        this.organName = organName;
    }

    public String getRemarkName() {
        return remarkName;
    }

    public void setRemarkName(String remarkName) {
        this.remarkName = remarkName;
    }

    public String getIsExiPreLink() {
        return isExiPreLink;
    }

    public void setIsExiPreLink(String isExiPreLink) {
        this.isExiPreLink = isExiPreLink;
    }
}
